<template name="stockEdit">
<view class="bgzd">
	<view class="form">
		<view class="form_top">
			<text>添加仓库物品</text>
			<image @click="close" src="/static/img/none_close.png"></image>
		</view>
		
		<view class="view_input">
		    <view class="view_input_title">物品名称:</view>
			<input class="view_input_input" type="text" v-model="item.name"
			placeholder-class="placeholderClass" placeholder="请输入物品名称">
		</view>
		<view class="view_input">
		    <view class="view_input_title">物品单价:</view>
			<input class="view_input_input" type="text" v-model="item.price"
			placeholder-class="placeholderClass" placeholder="请输入物品单价">
		</view>
		<view class="view_input">
		    <view class="view_input_title">物品数量:</view>
			<input class="view_input_input" type="text" v-model="item.num"
			placeholder-class="placeholderClass" placeholder="请输入物品数量">
		</view>
		<view class="view_input">
		    <view class="view_input_title">应付金额:</view>
			<input class="view_input_input" type="text" :value="item.num*item.price ? item.num*item.price : 0"
			placeholder-class="placeholderClass" disabled>
		</view>
		<view class="view_input">
		    <view class="view_input_title">实付金额:</view>
			<input class="view_input_input" type="text" v-model="item.pay_price"
			placeholder-class="placeholderClass" placeholder="请输入实付金额">
		</view>
		<view class="view_input">
			<view class="view_input_title">物品分类:</view>
			<picker class="view_input_input" @change="bindNameIndex" :value="NameIndex" :range="cate" 
			range-key="name">
				<view>
					{{cate[NameIndex]['name']}}
					<image class="picker_img" src="/static/img/buttom.png"></image>
				</view>
			</picker>
		</view>
		<!--<view class="view_input">
			<view class="view_input_title">物品图片:</view>
		</view>
		<image class="frame_img" v-if="item.img" @click="upLoad" :src="imgUrl+item.img"></image>
		<image class="frame_img" v-else @click="upLoad" src="/static/img/pub.png"></image>-->
		<view class="form_footer">
			<button v-if="addTrue" @click="addStock" class="strue">添加</button>
			<button v-else class="sfalse">添加</button>
		</view>
	</view>
</view>
</template>
<script>
import request from '@/util/request.js'
export default {
	name: "stockEdit",
	//属性
	props:[],
	data() {
		return {
			imgUrl:this.url,
			cate:[
					{'id':1,'name':'院线产品'},
					{'id':2,'name':'家居产品'}
			    ],
			NameIndex:0,
			name:'',
			addTrue:true,
			item:{
				//img:''
				type:1
			}
		}
	},
	//组件生命周期
	created:function(){
	},
	methods:{
		close:function(){
			this.$emit('func',{'type':1,'model':'stockAddShow'})
		},
		addStock:function(){
			if(!this.item.name){
				uni.showToast({
					title: '请填写物品名称!!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			if(!this.item.price){
				uni.showToast({
					title: '请填写物品单价!!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			if(this.item.num < 0){
				uni.showToast({
					title: '物品数量不能小于0!!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			if(!this.item.pay_price){
				uni.showToast({
					title: '请填写物品支付价格!!!',
					icon: "none", 
					position:'bottom',
				});
				return;
			}
			// if(!this.item.img){
			// 	uni.showToast({
			// 		title: '物品图片必填上传!!!',
			// 		icon: "none", 
			// 		position:'bottom',
			// 	});
			// 	return;
			// }
			this.addTrue = false
			this.item.oa_id = uni.getStorageSync('oa_id')
			var that = this
			request({
				url: 'manage/goods/add',
				method: 'post',
				data:this.item,
				success: (res) => {
					if(res.data.code == 1){
						uni.showToast({
							title: '添加成功!!!',
							icon: "none", 
							position:'bottom',
						});
						that.$emit('func',{'type':1,'model':'stockAddShow'})
					}else{
						uni.showToast({
							title: res.data.msg,
							icon: "none", 
							position:'bottom',
						});
					}
				},
			})
		},
		bindNameIndex:function(e){
			this.NameIndex = e.detail.value
			this.item.type = this.cate[e.detail.value]['id']
		},
		upLoad:function(){
			var token = 'tymy';
			var that = this
			uni.chooseImage({
				count: 1,
				sizeType:['copressed'],
				success(res){
					uni.showLoading({
					    title: '上传中'
					});
					//因为有一张图片， 输出下标[0]， 直接输出地址
					var imgFiles = res.tempFilePaths[0]
					var uper = uni.uploadFile({
						// 需要上传的地址
						url:that.imgUrl+'/api/app/upload',
						// filePath  需要上传的文件
						filePath: imgFiles,
						name:'img',
						header: {
						    // "Content-Type": "multipart/form-data",
						    // 'accept': 'application/json',  //若有token，此处换上你的token，没有的话省略
							'Authorization' : 'Bearer' + token
						},
						formData:{
							//name:'img'
						},
						success(res1) {
							uni.hideLoading();
							// console.log('上传结果',res1)
							// return;
							// 显示上传信息
							that.item['img'] = res1.data
						}
					});
				}
			})
		}
	},
}
</script>
<style>
.bgzd{
	position: fixed;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	width: 100%;
	height: 100%;
	background: rgba(0,0,0,0.4);
	z-index:999;
}
.form{
    padding-top:20rpx;
    padding-left:2%;
    width:100%;
    height:60%;
    bottom:0;
    padding-bottom: 20rpx;
    background:#d9d9d9;
    position: fixed;
    z-index:9999;
}
.form_top{
    font-weight:bold;
    font-size:20px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    width:95%;
}
.form_top image{
    width:35rpx;height:35rpx;
    margin-right: 20rpx;
}
.form_footer{
    width:95%;
    margin-top:60rpx;
}
.picker_img{width:35rpx;height:35rpx;}
.strue{
	width:100%;
	height:80rpx;
	line-height:80rpx;
	background:#ff861a;
	color:#fff;
}
.sfalse{
	width:100%;
	height:80rpx;
	line-height:80rpx;
	background:#8c8c8c;
	color:#fff;}
.frame_img{
	width:250rpx;height:200rpx;
	border:1rpx dashed #000000;
}
</style>